package com.example.javaee.controller;


import com.example.javaee.Util.JwtTokenUtil;
import com.example.javaee.domain.User;
import com.example.javaee.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    LoginService loginservice;
    @Autowired
    JwtTokenUtil jwtTokenUtil;


    @GetMapping("/kaptcha")
    public void getKaptcha(HttpServletResponse response){        // 生成验证码
        loginservice.getKaptcha(response);
    }

    @GetMapping("/login")
    public ResponseEntity<String> login(@RequestParam("username") String username,
                                        @RequestParam("password") String password) {
        if (loginservice.login(username, password)) {
            final String token = jwtTokenUtil.generateToken(username);
            return ResponseEntity.ok(token);
        }
        return ResponseEntity.badRequest().build();
    }

    /**
     * 获取用户信息名称
     * @return
     */
    @GetMapping("/getUserInfo")
    public User getUserInfo(){
        //通过用户工具类获取用户信息,
        User user = loginservice.getLoginUser();
        //用户未登录返回提示

        return user;
    }


}
